import 'package:dingtalk/core/model/chat_model.dart';
import 'package:dingtalk/core/utils/common_util.dart';
import 'package:dingtalk/core/values/values.dart';
import 'package:flutter/material.dart';

class THChatItemView extends StatefulWidget {
  final THChatModel model;

  THChatItemView({@required this.model});

  @override
  _THChatItemViewState createState() => _THChatItemViewState();
}

class _THChatItemViewState extends State<THChatItemView> {
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: kSize20, vertical: kSize20),
      child: widget.model.isSend == 1 ? buildSenderItem() : buildReceiveItem(),
    );
  }

  Widget buildReceiveItem() {
    return Row(
      children: [
        _buildAvatar(),
        Flexible(
          child: Container(
              margin: EdgeInsets.only(left: kSize10),
              child: _buildChatMessage()),
        )
      ],
    );
  }

  Widget buildSenderItem() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.end,
      children: [
        Flexible(
          child: Container(
              margin: EdgeInsets.only(right: kSize10),
              child: _buildChatMessage()),
        ),
        _buildAvatar(),
      ],
    );
  }

  Widget _buildAvatar() {
    return ClipOval(
      child: Container(
        width: kSize80,
        height: kSize80,
        alignment: Alignment.center,
        color: kTHPrimary,
        child: Text(
          geneChatName(widget.model.userName ?? ""),
          overflow: TextOverflow.ellipsis,
          maxLines: 1,
          style: TextStyle(
            color: Colors.white,
            fontSize: kSize28,
          ),
        ),
      ),
    );
  }

  Widget _buildChatMessage() => Container(
      padding: EdgeInsets.all(kSize20),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(kSize16)),
          boxShadow: [BoxShadow(color: kColorECEDED)]),
      child: Text(
        widget.model.message ?? "",
        softWrap: true,
        style: TextStyle(fontSize: kSize28),
      ));
}
